package net.maku.controller;

/*
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;

import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.core.MatOfByte;
import org.opencv.core.MatOfRect;
import org.opencv.core.Point;
import org.opencv.core.Rect;
import org.opencv.core.Scalar;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;
import org.opencv.objdetect.CascadeClassifier;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import cn.binarywang.wx.miniapp.api.WxMaService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.validation.Valid;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import net.maku.bean.param.WeixinParam;
import net.maku.bean.vo.TestResultVO;
import net.maku.bean.vo.UserTokenVo;
import net.maku.config.WxConfigProperties;
import net.maku.service.impl.LoginInfoServiceImpl;
import net.maku.utils.Result;

@Slf4j
@RestController
@AllArgsConstructor
@Tag(name="opencv处理")
public class OpencvController {


	
	
	
	@RequestMapping(value = "/testopencv", headers = "content-type=multipart/form-data")
    @Operation( summary = "人脸识别测试" )
	public Result<String>  fileUpload( HttpServletRequest  req, 
			@RequestPart("file") MultipartFile upfile ) throws Exception {
	//@Operation(summary = "人脸识别测试")
    //@RequestMapping("/testopencv")
    //public Result<String> wxUserLogin() throws Exception {
		
		
		System.out.println( upfile.getOriginalFilename() );
		System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
        
        // Load the cascade classifier file
		String xml = "/home/jishu/soft/opencv-4.10.0/data/haarcascades_cuda/haarcascade_frontalface_alt.xml";
		//String xml = "D:/download/opencv/winopencv/opencv/sources/data/haarcascades/haarcascade_frontalface_alt.xml";
        CascadeClassifier faceDetector = new CascadeClassifier(xml);
        
        // Read the image
        String img= "/home/jishu/nginx/webroot/images/logo.png";
        //Mat image = Imgcodecs.imread(img);
        
        //InputStream inputStream = new FileInputStream(img);
        / *
        InputStream inputStream=null;
        inputStream = new FileInputStream(img);
		byte[] fileContent = new byte[inputStream.available()];
	    inputStream.read(fileContent);
	    * /
        
        
	        
	        
 
	    // 将字节数组包装到MatOfByte中
	    MatOfByte matOfByte = new MatOfByte(upfile.getBytes());
	        
		//Mat image = Imgcodecs.imdecode(new Mat(matOfByte), Imgcodecs.IMREAD_COLOR);
	    Mat image = Imgcodecs.imdecode(matOfByte, Imgcodecs.IMREAD_COLOR);
	        

		 // Detect faces in the image
        MatOfRect faceDetections = new MatOfRect();
        faceDetector.detectMultiScale(image, faceDetections);
       
        
        // Draw rectangles around detected faces
        for (Rect rect : faceDetections.toArray()) {
            Imgproc.rectangle(image, new Point(rect.x, rect.y), 
                new Point(rect.x + rect.width, rect.y + rect.height), 
                new Scalar(0, 255, 0), 3);
        }
        
        System.out.println(faceDetections.toArray().length);
        
        System.out.println("==================end======================");
        return Result.ok(faceDetections.toArray().length+"");
	}
	
	
	public static void main(String[] args) {
        // Step 1: Load the OpenCV native library
        System.loadLibrary(Core.NATIVE_LIBRARY_NAME);

        // Step 2: Load the image file
        String imagePath = "D:/generator/world.jpg";  // Replace with your image path
        Mat image = Imgcodecs.imread(imagePath);

        if (image.empty()) {
            System.out.println("Error: Could not load image at " + imagePath);
            return;
        }

        // Step 3: Load the Haar Cascade file for face detection
        //"D:/download/opencv/winopencv/opencv/sources/data/haarcascades/haarcascade_frontalface_alt.xml"
        String cascadeFile = "D:/download/opencv/winopencv/opencv/sources/data/haarcascades/haarcascade_frontalface_alt.xml";  // Replace with the path to haarcascade_frontalface_default.xml
        CascadeClassifier faceDetector = new CascadeClassifier(cascadeFile);

        if (faceDetector.empty()) {
            System.out.println("Error: Could not load Haar Cascade XML file.");
            return;
        }

        // Step 4: Detect faces
        MatOfRect faceDetections = new MatOfRect();
        faceDetector.detectMultiScale(image, faceDetections);

        // Step 5: Draw rectangles around detected faces
        for (Rect rect : faceDetections.toArray()) {
            Imgproc.rectangle(image, 
                              new org.opencv.core.Point(rect.x, rect.y), 
                              new org.opencv.core.Point(rect.x + rect.width, rect.y + rect.height), 
                              new Scalar(0, 255, 0), 3);
        }

        // Step 6: Save the output image
        String outputImagePath = "D:/generator/output_image.jpg";  // Output path
        Imgcodecs.imwrite(outputImagePath, image);
        System.out.println("Detected faces saved to: " + outputImagePath);
    }

}
*/